package com.yuke.cloud.service.tmc.service.impl;

import com.google.common.base.Preconditions;
import com.yuke.cloud.service.tmc.entity.WebSocketServerMessage;
import com.yuke.cloud.service.tmc.service.FeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Service;

@Service
public class FeedServiceImpl implements FeedService {

    @Autowired
    private SimpMessageSendingOperations messaging;

//    @Autowired
//    public FeedServiceImpl(SimpMessageSendingOperations messaging) {
//        this.messaging = messaging;
//    }

    /**
     * 广播消息，不指定用户，所有订阅此的用户都能收到消息
     */
    @Override
    public void broadcastMsg(String dest, String msg) {
        Preconditions.checkArgument(dest != null, "消息目的地不能为空");
        Preconditions.checkArgument(msg != null, "消息不能为空");
        messaging.convertAndSend(dest, new WebSocketServerMessage(msg));
    }

    /**
     * 发送消息给指定用户（用户ID）
     */
    @Override
    public void SendMsgToUser(String dest, String userId, String msg) {
        Preconditions.checkArgument(dest != null, "消息目的地不能为空");
        Preconditions.checkArgument(userId != null, "用户ID不能为空");
        Preconditions.checkArgument(msg != null, "消息不能为空");
        messaging.convertAndSendToUser(userId, dest, new WebSocketServerMessage(msg));
    }
}
